<?php

namespace Gnway\Ticket\Rest\Facades;

use Gnway\Ticket\Rest\Container\RestContainer;
use ReflectionException;
use RuntimeException;

abstract class Facade
{

  /**
   * Resolve Facade Instance
   *
   * @param string $facadeAccessor
   * @return mixed|object
   * @throws ReflectionException
   */
  protected static function resolveFacadeInstance(string $facadeAccessor)
  {
    return RestContainer::make($facadeAccessor);
  }

  /**
   * Get Facade Root
   *
   * @return mixed|object
   * @throws ReflectionException
   */
  public static function getFacadeRoot()
  {
    return static::resolveFacadeInstance(static::getFacadeAccessor());
  }

  /**
   * @return string
   */
  abstract protected static function getFacadeAccessor(): string;

  /**
   * Facade Proxy
   *
   * @param mixed $name
   * @param mixed $arguments
   * @return mixed
   * @throws ReflectionException
   */
  public static function __callStatic($name, $arguments)
  {
    $instance = static::getFacadeRoot();
    if ( ! $instance) {
      throw new RuntimeException('A facade root has not been set.');
    }

    return $instance->$name(...$arguments);
  }
}